<?php

namespace App\Exceptions;

use App\Response;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        UserException::class,   // 自定义Exception
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render( $request, Exception $exception )
    {
        // debug模式
        if( env( 'APP_DEBUG', false ) )
        {
            return Response::json( [
                'code' => -1,
                'message' => 'service is busy',
                'debug' => [
                    'code' => $exception->getCode(),   // 错误码
                    'line' => $exception->getLine(),   // 错误行
                    'message' => $exception->getMessage(),  // 错误信息
                    'file' => $exception->getFile()    // 错误文件
                ]
            ] );
        }

        // 各个业务异常处理机制
        if( $exception instanceof UserException )
        {
            return $exception->render();
        }

        // 默认异常
        return Response::json( [ 'code' => '-1', 'message' => 'document not found' ] );

        // return parent::render($request, $exception);
    }
}
